package com.uroad.lib.widget.dialog;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uroad.lib.R;
import com.uroad.lib.adapter.lv.CommonAdapter;
import com.uroad.lib.adapter.lv.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class ChoosePopupWindow {

    private PopupWindow popupWindow;
    private LinearLayout ll_popup;
    private RelativeLayout rl_pop_parent;
    private ListView lv_pop;
    private Activity activity;
    private List<String> msgs = new ArrayList<String>();
    private CommonAdapter<String> adapter;
    private boolean closeing = false;

    public void initPopupWindow(Activity c, String title,
                                final Itemclick listener) {
        activity = c;
        popupWindow = new PopupWindow(c);
        View popView = c.getLayoutInflater().inflate(R.layout.pop_common, null);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setContentView(popView);
        ll_popup = (LinearLayout) popView.findViewById(R.id.ll_popup);
        rl_pop_parent = (RelativeLayout) popView
                .findViewById(R.id.rl_pop_parent);
        TextView tv_title = (TextView) popView.findViewById(R.id.tv_title);
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }

        lv_pop = (ListView) popView.findViewById(R.id.listview);
        adapter = new CommonAdapter<String>(activity, R.layout.view_item_pop_common, msgs) {
            @Override
            protected void convert(ViewHolder viewHolder, String item, int position) {
                viewHolder.setText(R.id.tvContent, item);
            }
        };
        lv_pop.setAdapter(adapter);
        lv_pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                listener.OnItemClickListener(arg2);
                popupWindow.dismiss();
            }
        });

        final Animation animation = AnimationUtils.loadAnimation(activity,
                R.anim.pop_push_bottom_out);
        rl_pop_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!closeing){
                    closeing = true;
                    ll_popup.startAnimation(animation);
                }
            }
        });

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                closeing = false;
                popupWindow.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void showPopupWindow(List<String> list) {
        if(list!=null&&list.size()>5){
            ViewGroup.LayoutParams layoutParams =  ll_popup.getLayoutParams();
            layoutParams.height = getScreenHeight(activity)/2;
        }
        msgs.clear();
        msgs.addAll(list);
        adapter.notifyDataSetChanged();
        popupWindow.showAtLocation(rl_pop_parent, Gravity.BOTTOM, 0, 0);
        ll_popup.startAnimation(AnimationUtils.loadAnimation(activity,
                R.anim.pop_push_bottom_in));
    }

    public interface Itemclick {
        void OnItemClickListener(int index);
    }

    /***
     * 获取屏幕高度
     *
     * @param activity
     * @return
     */
    public static int getScreenHeight(Activity activity) {
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.heightPixels;
    }
}
